#============================================================
#    Data Management
#============================================================
# - datetime: 2022-11-23 19:35:39
#============================================================
## 数据管理
class_name DataManagement
extends Node


##  数据发生改变
##[br]
##[br][code]property[/code]  属性名
##[br][code]previous[/code]  改变前的属性的值
##[br][code]current[/code]  当前的属性值
signal property_changed(property, previous, current)
## 新添加属性
signal newly_added_property(property, value)
## 移除了属性
signal removed_property(property, value)


var _data : Dictionary = {}
var _tmp_value


#============================================================
#  SetGet
#============================================================
##  获取所有数据
func get_all_data() -> Dictionary:
	return _data


##  设置属性值
##  set_property
##[br]
##[br][code]force_change[/code]  强制进行修改
func set_property(property, value, force_change: bool = false):
	_tmp_value = _data.get(property)
	if _data.has(property):
		if _tmp_value != value or force_change:
			_data[property] = value
			property_changed.emit(property, _tmp_value, value)
	else:
		_data[property] = value
		newly_added_property.emit(property, value)

##  获取属性值
##[br]
##[br][code]default[/code]  如果没有这个属性时返回的默认值
func get_property(property, default = null):
	return _data.get(property, default)

##  是否存在有这个属性
func has_property(property) -> bool:
	return _data.has(property)

##  添加属性
func add_property(property, value):
	if value is float or value is int:
		set_property(property, _data.get(property, 0) + value )
	else:
		set_property(property, value, true)

##  减去属性值
func sub_property(property, value):
	if value is float or value is int:
		set_property(property, _data.get(property, 0) - value )
	else:
		set_property(property, value, true)

##  移除属性值
func remove_property(property):
	if _data.has(property):
		removed_property.emit(property, _data[property])
		_data.erase(property)
